package pl.edu.pw.MidEmu.UI.Controllers;

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JMenuItem;

import pl.edu.pw.MidEmu.Base.Controllers.BaseController;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.Sensing.Sensors.ASensor;
import pl.edu.pw.MidEmu.Sensing.Sensors.DisplaySensor;
import pl.edu.pw.MidEmu.UI.Views.ShowSensorsView;

/**
 * Controller for adding new sensors
 * @author Mateusz Starodab
 *
 */
public class ShowSensorsController extends BaseController<ShowSensorsView> implements ActionListener, MouseListener, WindowListener
{
	private ASensor selectedSensor = null;

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() instanceof JButton)
		{
			JButton button = (JButton) e.getSource();
			if (button.getName() == Consts.ShowSensors_OkButton)
			{
				getView().dispose();
			}
		}
		if (e.getSource() instanceof JMenuItem)
		{
			JMenuItem item = (JMenuItem) e.getSource();
			if (item.getName() == Consts.ShowSensors_StopSensorMenu)
			{
				if (selectedSensor != null)
				{
					selectedSensor.pause();
					selectedSensor = null;
				}
			}
			if (item.getName() == Consts.ShowSensors_StartSensorMenu)
			{
				if (selectedSensor != null)
				{
					selectedSensor.resume();
					selectedSensor = null;
				}
			}
			else if (item.getName() == Consts.ShowSensors_ViewSensorData)
			{
				if (selectedSensor != null)
				{
					Point point = MouseInfo.getPointerInfo().getLocation();
					getView().showSensorDataWindow(selectedSensor, point);
					selectedSensor = null;
				}
			}
			else if (item.getName() == Consts.ShowSensors_AddSensor)
			{
				Point point = MouseInfo.getPointerInfo().getLocation();
				getView().showAddSensorWindow(point);
				selectedSensor = null;
			}
			else if (item.getName() == Consts.ShowSensors_RemoveSensorMenu)
			{
				if (selectedSensor != null)
				{
					getView().removeSensor(selectedSensor);
					selectedSensor = null;
				}
			}
		}

	}

	@Override
	public void mouseClicked(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		if (e.getButton() == MouseEvent.BUTTON3)
		{
			if (e.getSource() instanceof JList)
			{
				JList list = (JList) e.getSource();
				int index = list.locationToIndex(e.getPoint());
				boolean isInside = list.getCellBounds(index, index).contains(e.getPoint());
				if (isInside)
					selectedSensor = ((DisplaySensor) list.getModel().getElementAt(index)).getSensor();
				else
					selectedSensor = null;
				getView().showPopupMenu(e.getComponent(), e.getX(), e.getY(), selectedSensor);
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void windowActivated(WindowEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent e)
	{
		getView().rebindList();
	}

	@Override
	public void windowClosing(WindowEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent e)
	{
		// TODO Auto-generated method stub

	}

}
